﻿Imports System
Imports System.Collections
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports LibraryFiles


Public Class Comic
    Implements IDisposable


    Private _pages As ComicPages

    Public Property ArchiveType As ArchiveType
    Public Property TempFolder As String

    ' Comic Information From XML file 
    Public Property Title As String
    Public Property Series As String
    Public Property Number As String
    Public Property Count As Integer
    Public Property Volume As Integer
    Public Property AlternateSeries As String
    Public Property AlternateNumber As String
    Public Property AlternateCount As Integer
    Public Property Summary As String
    Public Property Notes As String
    Public Property Year As Integer
    Public Property Month As Integer
    Public Property Writer As String
    Public Property Penciler As String
    Public Property Colorist As String
    Public Property Letterer As String
    Public Property CoverArtist As String
    Public Property Editor As String
    Public Property Publisher As String
    Public Property Imprint As String
    Public Property Genre As String
    Public Property Web As String
    Public Property PageCount As Integer
    Public Property LanguageISO As String
    Public Property Format As String
    Public Property BlackAndWhite As Boolean
    Public Property Manga As Boolean



    Private _fileList As New List(Of String)
    Private actualIndex As Integer = -1
    Private disposedValue As Boolean = False        ' To detect redundant calls
    Public readMePath As String = String.Empty

    Public Sub New(ByVal fileName As String)
        Me.IsFavorite = False
        Path = fileName
        If IO.File.Exists(Path) Then
            Dim ext As String = IO.Path.GetExtension(Me.Path)
            ext = Replace(ext, ".", "")
            Select Case LCase(Trim(ext))
                Case "cbz", "zip", "gzip"
                    ArchiveType = ArchiveType.CBZ
                Case "rar", "cbr"
                    ArchiveType = ArchiveType.CBR
                Case "pdf"
                    ArchiveType = ArchiveType.PDF
                Case Else
                    ArchiveType = ArchiveType.Unknown
                    If FileChecker.CheckSignature(fileName, 7, "52-61-72-21-1A-07-00") Then ArchiveType = ArchiveType.CBR : Exit Select ' RAR checking 
                    If FileChecker.CheckSignature(fileName, 3, "1F-8B-08") Then ArchiveType = ArchiveType.CBZ : Exit Select ' gzip checking 
                    If FileChecker.CheckSignature(fileName, 4, "50-4B-03-04") Then ArchiveType = ArchiveType.CBZ : Exit Select ' PK-Zip Checking 
            End Select ' Case LCase(Trim(ext)) 

            Me.FileName = IO.Path.GetFileNameWithoutExtension(fileName)
            Dim str As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
            TempFolder = str & "/archives/" & IO.Path.GetFileNameWithoutExtension(fileName)
            If Not IO.Directory.Exists(TempFolder) Then IO.Directory.CreateDirectory(TempFolder)
        ElseIf IO.Directory.Exists(fileName) Then
            ArchiveType = ArchiveType.OpenDirectory
            Me.TempFolder = String.Empty
        Else
            Me.TempFolder = String.Empty
            Throw New FileNotFoundException
        End If

    End Sub

    Public Property FileName As String
    Public Property Path As String

    Public ReadOnly Property ComicPages() As ComicPages
        Get
            If _pages Is Nothing Then _pages = New ComicPages
            Return _pages
        End Get
    End Property


    Public Property IsFavorite As Boolean

    Public Sub ProcessXmlFile(ByVal xmlFilePath As String)

    End Sub

    Public Sub ProcessNotesFile(ByVal notesFilePath As String)

    End Sub

  
#Region " IDisposable Support "

    Protected Sub cleanupFolders(ByVal folder As String)
        If Not String.IsNullOrEmpty(folder) AndAlso IO.Directory.Exists(folder) Then
            For Each dn As String In IO.Directory.GetDirectories(folder)
                cleanupFolders(dn)
            Next
            For Each fn As String In IO.Directory.GetFiles(folder)
                Try
                    IO.File.Decrypt(fn)
                Catch ex As Exception
                End Try
            Next
            Try
                IO.Directory.Delete(folder)
            Catch ex As Exception
            End Try
        End If
    End Sub
    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: free other state (managed objects).
            End If
            cleanupFolders(Me.TempFolder)
        End If
        Me.disposedValue = True
    End Sub


    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

Public Enum ArchiveType
    CBZ
    CBR
    PDF
    OpenDirectory
    Unknown
End Enum